﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class ModulesController : ModulesControllerBase{
	public ModulesController()
		: base()
	{
	}	
	private DataTable _Table;
	private ModulesEntity oModules;	
	public ModulesEntity ModulesObject{
		get{
			return oModules;
		}
		set{
			oModules=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSModules(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 ModuleId){
		ModulesEntity oLoad=FindModules(ModuleId);
		if(oLoad==null){
			LoadAllModules(true);
			oModules=FindModules(ModuleId);
		}
		else{
			oModules=oLoad;
		}
	}
	public bool AddNew(){
		if(oModules==null)return false;
		bool result = Service.Save(oModules);
		if(result){
			AddObjectToCache(oModules);
		}
		return result;
	}
	public bool Update(){
		if(oModules==null)return false;
		return Service.Update(oModules);	
	}
	public bool Delete(){
		if(oModules==null)return false;		
		bool retvalue = Service.Delete(oModules);
		if(retvalue){			
			RemoveObjectFromCache(oModules);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 ModuleId){
		bool retvalue = Service.DeleteByID(ModuleId);		
		if(retvalue){
			ModulesEntity oDeleted=FindModules(ModuleId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oModules = new ModulesEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ModulesObject,row);
    }
    public ModulesEntity LoadModules(Int32 ModuleId){			
		ModulesEntity o=Service.SelectOne(ModuleId);		
		return o;
	}
	#region Cached
    private static List<ModulesEntity>_ListModules;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="ModulesDS";
	private const string CACHE_KEY_LS="ModulesLS";
	private void AddObjectToCache(ModulesEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(ModulesEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<ModulesEntity>GetCacheList(){
		if(Cache==null)return default(List<ModulesEntity>);
		List<ModulesEntity>ls= Cache.Get<List<ModulesEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<ModulesEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}	
	public static Dictionary<Int32,ModulesEntity> GetModulesDictionary(){
		Dictionary<Int32,ModulesEntity> dic=new Dictionary<Int32,ModulesEntity>();
		foreach(ModulesEntity o in ListModulesStatic){
			dic.Add(o.ModuleId,o);
		}
		return dic;
	}
	public List<ModulesEntity>ListModules{
		get{
			return LoadAllModules(false);
		}
	}
	public static List<ModulesEntity>ListModulesStatic{
		get{
			LoadAllModules(false);
			return _ListModules;
		}
	}
	public ModulesEntity FindModules(Int32 ModuleId){
		if(AllowCaching){
			_ListModules=GetCacheList();
		}		
		ModulesEntity oResult=_ListModules.Find(delegate(ModulesEntity oFind){
			bool result=true; 
			result&=oFind.ModuleId.Equals(ModuleId);
			return result;
		 });
		return oResult;
	}
	public static List<ModulesEntity>LoadAllModules(bool clearCache){
		_ListModules = new List<ModulesEntity>();
		if(AllowCaching){
			_ListModules=GetCacheList();
		}
		if(_ListModules==null||_ListModules.Count==0)
			clearCache=true;
		if(!clearCache)return _ListModules;
		_ListModules=new List<ModulesEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListModules);
		return _ListModules;
	}
	#endregion
}
}
